package edu.kit.csl.cratyli.control;

import java.util.Map;
import java.util.Map.Entry;

import edu.kit.csl.cratyli.datatypes.BadParameterValueException;
import edu.kit.csl.cratyli.datatypes.Word;

/**
 * This merger selects the pronunciation with the highest score. The score of
 * the primary word is the current temperature. The scores of the secondary
 * words are scaled by 1 - current temperature.
 */
public class MaximumMerger extends Merger {

	public MaximumMerger() throws BadParameterValueException {
		super();
	}

	/* (non-Javadoc)
	 * @see edu.kit.csl.cratyli.control.Merger#merge(Word, java.util.Map)
	 */
	@Override
	public void merge(Word pWord, Map<Word, Double> sWords) {
		double bestScore = annealing.getTemperature() * pWord.getWeight();
		Word bestWord = pWord;
		for (Entry<Word, Double> entry : sWords.entrySet()) {
			if (entry.getValue() > bestScore) {
				bestWord = entry.getKey();
				bestScore = entry.getValue();
			}
		}
		if (!bestWord.equals(pWord)) {
			pWord.setPronunciation(bestWord.getPronunciation());
			pWord.setOrthographic(bestWord.getOrthographic());
		}
	}
}
